package com.showde.shoppingmall.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.activity.MyStreetGroup;
import com.showde.shoppingmall.activity.ProductDetailsActivity;
import com.showde.shoppingmall.domain.Product;
import com.showde.shoppingmall.domain.ProductAHR;
import com.showde.shoppingmall.utils.ActivityDispatch;

public class HotProductViewPagerAdapter extends PagerAdapter {

	List<View> views = new ArrayList<View>();;
	List<ProductAHR> productAdvertisement = null;

	ImageLoader imageLoader;
	Context context;

	public HotProductViewPagerAdapter(Context context,
			List<ProductAHR> productAdvertisement) {
		this.context = context;
		this.productAdvertisement = productAdvertisement;
		imageLoader = new ImageLoader(context);
		generateViews();
	}

	private void generateViews() {
		for (final ProductAHR product : productAdvertisement) {
			ImageView imageView = new ImageView(context);
			imageView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Bundle bundle = new Bundle();
					bundle.putString(ActivityDispatch.PRODUCT_ID, product.getProductID());
					ActivityDispatch.openNewActivity(context, ProductDetailsActivity.class,
							MyStreetGroup.group, "ProductDetailsActivity", bundle,
							ActivityDispatch.GROUP_STREET);
				}
			});
			imageLoader.displayImage(product.getProductLogo(), imageView);
			views.add(imageView);
		}
	}

	@Override
	public int getCount() {
		return productAdvertisement.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		if (position >= views.size()) {
			return;
		}
		((ViewPager) container).removeView(views.get(position));
	}

	@Override
	public Object instantiateItem(View container, int position) {
		((ViewPager) container).addView(views.get(position), 0);
		return views.get(position);
	}

	@Override
	public int getItemPosition(Object object) {
		return PagerAdapter.POSITION_NONE;
	}

}
